package com.feather.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VideoUtil {
	/**
	 * 获取指定时间内的图片
	 * 
	 * @param videoFilename
	 *            :视频路径
	 * @param thumbFilename
	 *            :图片保存路径
	 * @param width
	 *            :图片长
	 * @param height
	 *            :图片宽
	 * @param hour
	 *            :指定时
	 * @param min
	 *            :指定分
	 * @param sec
	 *            :指定秒
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void getThumb(String ffmpeg, String videoFile, String thumbFile, int width, int height, int hour, int min, float sec) throws IOException, InterruptedException {
		ProcessBuilder processBuilder = new ProcessBuilder(ffmpeg, "-y", "-i", videoFile, "-vframes", "1", "-ss", hour + ":" + min + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height, "-an", thumbFile);

		Process process = processBuilder.start();

		InputStream stderr = process.getErrorStream();
		InputStreamReader isr = new InputStreamReader(stderr);
		BufferedReader br = new BufferedReader(isr);
		@SuppressWarnings("unused")
		String line = null;
		while ((line = br.readLine()) != null)
			;
		process.waitFor();

		if (br != null) {
			br.close();
		}
		if (isr != null) {
			isr.close();
		}
		if (stderr != null) {
			stderr.close();
		}
	}

	public static VideoInfo getInfo(String ffmpeg, String videoFile) throws IOException, InterruptedException {
		VideoInfo info = new VideoInfo();

		String tmpFile = videoFile + ".tmp.png";
		ProcessBuilder processBuilder = new ProcessBuilder(ffmpeg, "-y", "-i", videoFile, "-vframes", "1", "-ss", "0:0:0", "-an", "-vcodec", "png", "-f", "rawvideo", "-s", "100*100", tmpFile);

		Process process = processBuilder.start();

		InputStream stderr = process.getErrorStream();
		InputStreamReader isr = new InputStreamReader(stderr);
		BufferedReader br = new BufferedReader(isr);
		String line;
		// 打印 sb，获取更多信息。 如 bitrate、width、heigt
		StringBuffer sb = new StringBuffer();
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}

		new File(tmpFile).delete();

		// System.out.println("video info:\n" + sb);
		Pattern pattern = Pattern.compile("Duration: (.*?),");
		Matcher matcher = pattern.matcher(sb);

		if (matcher.find()) {
			String time = matcher.group(1);
			calcTime(info, time);
		}

		pattern = Pattern.compile("w:\\d+ h:\\d+");
		matcher = pattern.matcher(sb);

		if (matcher.find()) {
			String wh = matcher.group();// w:100 h:100
			String[] strs = wh.split("\\s+");
			if (strs != null && strs.length == 2) {
				info.width = Integer.parseInt(strs[0].split(":")[1], 10);
				info.heigt = Integer.parseInt(strs[1].split(":")[1], 10);
			}
		}

		process.waitFor();
		if (br != null) {
			br.close();
		}
		if (isr != null) {
			isr.close();
		}
		if (stderr != null) {
			stderr.close();
		}
		return info;
	}

	private static void calcTime(VideoInfo info, String timeStr) {
		String[] parts = timeStr.split(":");
		info.hours = Integer.parseInt(parts[0], 10);
		info.minutes = Integer.parseInt(parts[1], 10);
		info.seconds = Float.parseFloat(parts[2]);
	}

	public static void main(String[] args) {
		String ffmpeg = "D:\\work\\provider\\mw\\ffmpeg\\bin\\ffmpeg.exe";
		try {
			VideoUtil.getThumb(ffmpeg, "D:/J0KCQZ7W_7792770.mp4", "D:\\thumbTest.png", 800, 600, 0, 0, 1);
			System.out.println("over");
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			// D:/work/media/webedu/video/20131216/1387176561658.mov
			VideoInfo videoInfo = VideoUtil.getInfo(ffmpeg, "D:/work/media/webedu/video/20131216/1387176573283.mp4");// 1387176573283.mp4
			System.out.println(videoInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
